package com.iplant.mes.service.po.bcm;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.enums.DBSchemas;
import com.iplant.base.utils.annotation.po.DBBasePO;
import com.iplant.base.utils.annotation.field.DBField;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.mes.service.mesenum.bcm.BCMRuleItemTypes;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;

@Slf4j
@DBBasePO(TableName = "bcm_rule_item", Schemas = DBSchemas.MES,
        CheckName = {"Code"}, CodeAuto = true, CodePrefix = "BCMR-")
public class BCMRuleItem extends BasePO {


    /**
     * 规则字符串   订单 产品 工厂代码 此字段为截取起始位
     */
    @DBField
    public String RuleString = "";


    /**
     *
     */
    @DBField
    public int RuleItemType = 0;

    /**
     * 截取长度
     */
    @DBField
    public int Length = 2;

    /**
     * 截取位置
     */
    @DBField
    public int ValueIndex = 0;


    /**
     * 截取模式  0头部 1尾部
     */
    @DBField
    public int AroundType = 0;

    @DBField
    public int SequenceNo = 1;


    private String getRuleCodeString(String wValue) {
        if (Length <= 0)
            Length = 1;
        if (wValue == null || wValue.length() <= ValueIndex) {
            return StringUtils.PadLeft(wValue, Length, '_');
        }
        if (AroundType == 1) {
            if (wValue.length() < (ValueIndex + Length)) {
                return StringUtils.PadLeft(wValue.substring(0, wValue.length() - ValueIndex), Length, '_');

            } else {
                return wValue.substring(wValue.length() - ValueIndex - Length, wValue.length() - ValueIndex);
            }
        } else {
            if (wValue.length() < (ValueIndex + Length)) {
                return StringUtils.PadRight(wValue.substring(ValueIndex), Length, '_');
            } else {
                return wValue.substring(ValueIndex, ValueIndex + Length);
            }
        }

    }

    public String getRuleCodeString(String wOrderNo, String wProductNo,String wProductSimplifiedCode,  String wFactoryCode) {
        String wRuleCodeString = "";
        switch (BCMRuleItemTypes.getEnumType(RuleItemType)) {

            case Factory:
                wRuleCodeString = getRuleCodeString(wFactoryCode);
                break;
            case Order:
                wRuleCodeString = getRuleCodeString(wOrderNo);
                break;
            case Product:
                wRuleCodeString = getRuleCodeString(wProductNo);
                break;
            case ProductSimplified:
                wRuleCodeString = getRuleCodeString(wProductSimplifiedCode);
                break;
            case Date:
                if (StringUtils.isEmpty(RuleString))
                    RuleString = BCMRuleItemTypes.Date.getRemark();
                wRuleCodeString = StringUtils.parseCalendarToString(Calendar.getInstance(), RuleString);
                break;
            case Sequence:
                if (Length <= 0)
                    Length = 7;
                wRuleCodeString = StringUtils.PadLeft("1", Length, '0');
                break;
            case Default:
            default:
                wRuleCodeString = StringUtils.PadLeft(RuleString, Length, '_');
                break;

        }
        return wRuleCodeString;
    }

    @JsonProperty("RuleString")
    public String getRuleString() {
        return RuleString;
    }

    public void setRuleString(String ruleString) {
        RuleString = ruleString;
    }

    @JsonProperty("RuleItemType")
    public int getRuleItemType() {
        return RuleItemType;
    }

    public void setRuleItemType(int ruleItemType) {
        RuleItemType = ruleItemType;
    }

    @JsonProperty("Length")
    public int getLength() {
        return Length;
    }

    public void setLength(int length) {
        Length = length;
    }

    @JsonProperty("SequenceNo")
    public int getSequenceNo() {
        return SequenceNo;
    }

    public void setSequenceNo(int sequenceNo) {
        SequenceNo = sequenceNo;
    }

    @JsonProperty("ValueIndex")
    public int getValueIndex() {
        return ValueIndex;
    }

    public void setValueIndex(int valueIndex) {
        ValueIndex = valueIndex;
    }

    @JsonProperty("AroundType")
    public int getAroundType() {
        return AroundType;
    }

    public void setAroundType(int aroundType) {
        AroundType = aroundType;
    }
}
